package com.example.maoyanmovie.viewmodel;

import androidx.lifecycle.MutableLiveData;
import com.example.maoyanmovie.base.BaseViewModel;
import com.example.maoyanmovie.model.Cinema;
import com.example.maoyanmovie.network.RetrofitClient;
import com.example.maoyanmovie.utils.Constants;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.util.List;

public class CinemaViewModel extends BaseViewModel {

    public MutableLiveData<List<Cinema>> cinemaListLiveData = new MutableLiveData<>();
    public MutableLiveData<String> errorLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();

    public void loadCinemas() {
        loadingLiveData.setValue(true);

        addDisposable(
                RetrofitClient.getInstance()
                        .getApiService()
                        .getCinemas(Constants.DEFAULT_CITY_ID)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                                response -> {
                                    loadingLiveData.setValue(false);
                                    if (response != null && response.getBrand() != null && response.getBrand().getSubItems() != null) {
                                        cinemaListLiveData.setValue(response.getBrand().getSubItems());
                                    }
                                },
                                throwable -> {
                                    loadingLiveData.setValue(false);
                                    errorLiveData.setValue(throwable.getMessage());
                                }
                        )
        );
    }
}
